package cn.dw.dao.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import cn.dw.dao.BaseDao;
import cn.dw.domain.DutyInfo;
import cn.dw.domain.People;

@SuppressWarnings({"rawtypes","unchecked"})

public class BaseDaoImpl<T> implements BaseDao<T> {
	private HibernateTemplate ht;
	private Class clazz;

	
	public BaseDaoImpl() {
		ParameterizedType superClazz = (ParameterizedType) getClass().getGenericSuperclass();

		this.clazz = ((Class) superClazz.getActualTypeArguments()[0]);
	}

	public void save(T t) {
		this.ht.save(t);
	}

	public void saveOrUpdate(T t) {
		this.ht.saveOrUpdate(t);
	}

	public void delete(T t) {
		this.ht.delete(t);
	}

	public void delete(Serializable id) {
		this.ht.bulkUpdate("delete from  " + this.clazz.getName() + "  where id = ? ", id);
	}

	public void update(T t) {
		this.ht.update(t);
	}

	
	public T getById(Serializable id) {
		return (T) this.ht.get(this.clazz, id);
	}

	public List<T> getByPointId(Serializable id) {
		List<T> list = this.ht.find(" from " + this.clazz.getName() + " where point_id=" + id);
		return list;
	}

	public List<T> getByObjectId(Object id, Object column) {
		List<T> list = this.ht.find(" from " + this.clazz.getName() + " where " + column + "=" + id);
		return list;
	}

	public Integer getCount() {
		List<Long> list = this.ht.find("select count(id) from " + this.clazz.getName() + "  ");

		return (list != null) && (list.size() > 0) ? Integer.valueOf(((Long) list.get(0)).intValue()) : null;
	}

	public List<T> getAll() {
		List<T> list = this.ht.find(" from " + this.clazz.getName() + "  ");
		return list;
	}

	public List<T> getByPage(final int index, final int total) {
		List<T> list = (List) this.ht.execute(new HibernateCallback() {
			public List<T> doInHibernate(Session session) throws HibernateException, SQLException {
				String hql = " from " + BaseDaoImpl.this.clazz.getName() + "  ";
				Query query = session.createQuery(hql);
				query.setFirstResult(index);
				query.setMaxResults(total);
				return query.list();
			}
		});
		return list;
	}

	public HibernateTemplate getHt() {
		return this.ht;
	}

	public List<T> getByConditions(DetachedCriteria dc) {
		return this.ht.findByCriteria(dc);
	}

	public void setHt(HibernateTemplate ht) {
		this.ht = ht;
	}

	public boolean getByOpenId(String OpenId) {
		List<T> list = ht.find(" from " + clazz.getName() + "where openid = " + OpenId);
		if (list != null) {
			return true;
		}
		return false;
	}

	public void Insert(String Hql) {
		Session session = this.ht.getSessionFactory().getCurrentSession();

		session.createSQLQuery(Hql).executeUpdate();
	}

	public DutyInfo getByPeople(People peo) {
		return null;
	}
}
